<?php
/* explain:		
 * Project:     
 * File:        vedioUpload.php
 *
 * @ link 		http://www.../lib/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2007  Ling Deming
 * @ author 	ivan.ling  Ling Deming
 * @ version 	1.0
 *
 */
include_once("Common.php");
include_once("Logs.php");
class UploadFiles extends Common
{
	const TYPE_IMAGE	= 1;
	
	private $intLinkSign;
	private $iUserID;
	
	private $objLog;
	
	function UploadFiles($DBTag, $usrid)
	{
		parent::Common();
		$this->intLinkSign = $this->getConnection($DBTag);
		
		$this->objLog 	= new Logs($usrid);
		$this->iUserID 	= $usrid;
	}

	/* get data via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function getByID($arrPopedom = array(), $id)
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		
		$sql = "SELECT * FROM upload_files WHERE id = $id";
		$rs  = mysql_query($sql, $this->intLinkSign);
		if($rs)
			return mysql_fetch_array($rs);
		else
			return false;
	}

	/* 
	 *
	 * @param  string 	name 
 	 * @return int    	true: insert ID
	 */
    public function add($arrPopedom = array(), $type, $extName, $md5, $width, $height, $fileSize)
	{
		if($this->isUnallowableAdd($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_ADD);
		$id = 0;
		
		$sql = "INSERT INTO upload_files SET createdTime = UTC_TIMESTAMP(), modifiedTime = UTC_TIMESTAMP(), type='$type', extName='$extName', md5='$md5', width = $width, height = $height, fileSize='$fileSize'";
		$rs  = mysql_query($sql, $this->intLinkSign) or die ($sql . mysql_error($this->intLinkSign));
		if($rs){
			$id = mysql_insert_id($this->intLinkSign);
			
			return $id;
		}else
			return $id;
	}
	
	public function addtmp($type, $extName, $md5, $width, $height, $fileSize)
	{
		$id = 0;
		
		$sql = "INSERT INTO upload_files SET createdTime = UTC_TIMESTAMP(), modifiedTime = UTC_TIMESTAMP(), type='$type', extName='$extName', md5='$md5', width = $width, height = $height, fileSize='$fileSize'";
		$rs  = mysql_query($sql, $this->intLinkSign) or die ($sql . mysql_error($this->intLinkSign));
		if($rs){
			$id = mysql_insert_id($this->intLinkSign);
			
			return $id;
		}else
			return $id;
	}

	public function isExistedtmp($type, $md5)
	{
		$sql = "SELECT id, extName, width, height, fileSize FROM upload_files WHERE type = $type AND md5 = '$md5'";
		
		$rs  = mysql_query($sql, $this->intLinkSign);
		if($rs && mysql_num_rows($rs) > 0){
			return mysql_fetch_array($rs);

		}else
			return 0;
	}
	
	/* modify
	 *
	 * @param  string 	name 
 	 * @param  return 	boolean
	 */
	public function edit($arrPopedom = array(), $id, $type, $extName, $md5, $fileSize)
	{
		if($this->isUnallowableEdit($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_EDIT, "picid = $picid AND formatid='$oldformatid'");
			
		$sql = "UPDATE upload_files SET type='$type', extName='$extName', md5='$md5', fileSize='$fileSize' WHERE id = $id";
		$rs	 = mysql_query($sql, $this->intLinkSign);
		if($rs)
			return true;
		else
			return false;
	}
	
	/* delete news class
	 *
	 * @param  int    	id
	 * @param  string  	status
	 * @param  return 	boolean
	 */
	public function delete($arrPopedom = array(), $id)
	{
		if($this->isUnallowableDelete($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_DELETE, "id = $id ");
			
		$sql = "DELETE FROM upload_files WHERE id = $id";
		$rs	 = mysql_query($sql, $this->intLinkSign);
		if($rs)
			return true;
		else
			return false;
	}
	
	/* get data via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function isExisted($arrPopedom = array(), $type, $md5)
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		
		$sql = "SELECT id FROM upload_files WHERE type = $type AND md5 = '$md5'";
		
		$rs  = mysql_query($sql, $this->intLinkSign);
		if($rs && mysql_num_rows($rs) > 0){
			$row = mysql_fetch_array($rs);
			
			return $row['id'];
		}else
			return 0;
	}
	
	/* list
	 *
	 * @ param  string 	class id
	 * @ param  string 	page
	 * @ param  string 	discount
	 * @ param  string 	url
	 * @ param  return 	array
	 */
	public function lists($arrPopedom = array(), $where, $page, $discount, $url = '')
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_VIEW);
			
		$arrback = array();

		$sql = 'SELECT count(*) FROM upload_files'.$where;

		$rs  = mysql_query($sql, $this->intLinkSign);
		list($total) = mysql_fetch_row($rs);
		$this->mutilPager($total, $page, $discount, $url);
		if(empty($total))
 			return $arrback;

		if(empty($discount))
			$sql = 'SELECT * FROM upload_files'.$where;
		else{
			$sql = 'SELECT * FROM upload_files'.$where." LIMIT $this->firstcount, $discount";
		}
		$rs  = mysql_query($sql, $this->intLinkSign);
		if($rs){
			while(is_array($arrtmp = mysql_fetch_array($rs))){
				$arrback[] = $arrtmp;
			}
			return $arrback;
		}else
 			return $arrback;
	}

}
?>
